D:\a\tools.proto\tools.proto\dynamic\src\component\interface.rs
Line | Count | Source |
1 | | // Copyright (c) 2025, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::buffer::{BufferView, Builder}; |
30 | | use crate::component::util::DiscoverTool; |
31 | | |
32 | | pub trait ComponentType { |
33 | | /// Returns the type name of this component. |
34 | 0 | fn name(&self) -> &str { |
35 | 0 | std::any::type_name::<Self>() |
36 | 0 | } |
37 | | |
38 | | /// Returns the key to use when comparing component types. |
39 | | /// If this returns 0 the key is assumed to be the pointer stored in the wrapping [Rc](std::rc::Rc). |
40 | 0 | fn key(&self) -> usize { |
41 | 0 | 0 |
42 | 0 | } |
43 | | |
44 | | /// Adds the necessary information to the given [Builder] to construct a [BufferView] |
45 | | /// representing this [ComponentType]. |
46 | | /// |
47 | | /// # Arguments |
48 | | /// |
49 | | /// * `builder`: the builder to complete. |
50 | | /// |
51 | | /// returns: Builder |
52 | | fn build(&self, builder: Builder<'static>) -> Builder<'static>; |
53 | | |
54 | | /// Creates a new [BufferView] representing this [ComponentType]. |
55 | | /// |
56 | | /// # Arguments |
57 | | /// |
58 | | /// * `init_mem`: true to pre-initialize the memory of this view with zeros, false otherwise. |
59 | | /// |
60 | | /// returns: BufferView |
61 | 7 | fn new_instance(&self, init_mem: bool) -> BufferView<'static> { |
62 | 7 | self.build(Builder::new(self.name())).build(init_mem) |
63 | 7 | } |
64 | | } |
65 | | |
66 | | pub trait Component { |
67 | | /// Reads the data given in the BufferView. |
68 | | /// |
69 | | /// # Arguments |
70 | | /// |
71 | | /// * `view`: the view to read from. |
72 | | /// * `items`: list of items to fill, clear it if no list is to be attached with the [BufferView]. |
73 | | /// |
74 | | /// returns: Result<(), Error> |
75 | | fn read(&self, view: &mut BufferView, items: &mut DiscoverTool) -> bp3d_proto::message::Result<()>; |
76 | | |
77 | | /// Shapes the given BufferView. |
78 | | /// |
79 | | /// # Arguments |
80 | | /// |
81 | | /// * `view`: the view to shape. |
82 | | /// * `items`: list of items attached with the [BufferView], this should be written to the |
83 | | /// underlying BufferView. |
84 | | /// |
85 | | /// returns: Result<(), Error> |
86 | | fn shape(&self, view: &mut BufferView, items: &Vec<BufferView>) -> bp3d_proto::message::Result<()>; |
87 | | } |